<template>
  <div class="login">
    <el-drawer :model-value="drawer" direction="rtl" append-to-body :before-close="handleClose">
      <template #header>
        <h2>登录</h2>
      </template>
      <template #default>
        <div class="container">
          <div class="avatar">
            <el-avatar :size="150" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" />
          </div>

          <el-form :model="form" :rules="rules" ref="ruleFormRef">
            <el-form-item prop="username">
              <el-input v-model="form.username" placeholder="用户名/电话号码/邮箱">
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <search />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="form.password" placeholder="请输入密码" type="password">
                <template #prefix>
                  <el-icon>
                    <Unlock />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-checkbox v-model="isKeep" label="记住密码" size="large" />
            </el-form-item>
            <el-form-item>
              <el-button @click="handleClose">取消</el-button>
              <el-button type="primary" @click="submitForm">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from "vue";
import type { FormInstance, FormRules } from 'element-plus'
import { useUser } from '@/store/user'

const props = defineProps({
  drawer: {
    type: Boolean,
    default: false,
  },
});


const isKeep = ref(false)
watch(() => props.drawer, (v) => {
  if (v) {
    isKeep.value = JSON.parse(localStorage.getItem('account') as any)?.isKeep || false
    form.username = JSON.parse(localStorage.getItem('account') as any)?.username || ''
    form.password = JSON.parse(localStorage.getItem('account') as any)?.password || ''
  }
})

const emit = defineEmits(["close"]);
const ruleFormRef = ref()
const form = reactive({
  username: "",
  password: "",
});
const rules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名、手机号或邮箱', trigger: 'blur' },
    { min: 3, message: '最小长度大于3字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '最小长度大于6字符', trigger: 'blur' },
  ],

})
const submitForm = async () => {
  (ruleFormRef.value as FormInstance).validate(async valid => {
    if (valid) {
      if (await userStore.login(form)) {
        if (isKeep.value) {
          localStorage.setItem('account', JSON.stringify({ ...form, isKeep: isKeep.value }))
        } else {
          localStorage.removeItem('account')
        }
      }

    } else {
      console.log('验证未通过');
    }
  })

}

const userStore = useUser()
const resetForm = () => {
  form.password = ''
  form.username = ''
}

const handleClose = () => {
  resetForm()
  emit("close");
};
function cancelClick() {
  emit("close");
}
</script>

<style scoped lang="scss">
.container {
  height: 100%;

  .avatar {
    text-align: center;
    margin-bottom: 40px;
  }
}
</style>
